package com.tanhua.backstage.controller;


import com.tanhua.backstage.vo.PageResult;
import com.tanhua.backstage.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tanhua.backstage.service.UsersService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @PackageName: com.tanhua.backstage.controller
 * @Classname: UserController
 * @Author: zlqjava@163.com
 * @Date: 2021/10/13 14:38
 */
@RestController
@RequestMapping("/manage/users")

public class UsersController {

    @Autowired
    private UsersService usersService;


    //查询用户基本资料，数据分页;
    @GetMapping
    public PageResult queryUserByPage(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                      @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize,
                                      @RequestParam(value = "id", required = false) Long id,
                                      @RequestParam(value = "nickname", required = false) String nickname,
                                      @RequestParam(value = "city", required = false) String city) throws Exception {
        return this.usersService.queryUserByPage(page, pageSize, id, nickname, city);
    }


    //查询用户详细资料;
    @GetMapping("{userID}")
    public ResponseEntity<Object> queryUserById(@PathVariable(name = "userID") Long userId) {
        //调用usersService根据id查询用户数据;
        UserVo userVo = this.usersService.queryUserById(userId);
        try {
            //将json数据响应到前端;
            return ResponseEntity.ok(new ObjectMapper().writeValueAsString(userVo));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        //响应失败状态;
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

}
